#ifdef WWWPAL
#include "HTRobMan.h"
#include "RobotXGMML.h"
#include "HTAncMan.h"

#define SHOW_QUIET(mr)		((mr) && !((mr)->flags & MR_QUIET))
#define SHOW_REAL_QUIET(mr)	((mr) && !((mr)->flags & MR_REAL_QUIET))



PUBLIC int 
is_title_of_directory(char *str)
{
    return(!strncmp(str,"Index of", 8));
}

PUBLIC BOOL
is_html_document(HTRequest *request)
{
    HTParentAnchor *panchor = HTRequest_anchor(request);

    HTFormat nformat = HTAnchor_format(panchor);

    return (nformat == WWW_HTML);
}

PUBLIC void
generate_web_graph(Robot *mr)
{
    if (mr->graphfile && (!mr->fgraph)) {
	if ((mr->fgraph = fopen(mr->graphfile, "w+")) == NULL) {
            if (SHOW_REAL_QUIET(mr)) HTPrint("Can't open `%s'\n", mr->graphfile\
		);
            mr->fgraph = OUTPUT;
	}
    }      

    get_xgmml_graph_robot(mr, mr->fgraph);

    fclose(mr->fgraph);
    mr->fgraph = NULL;
}

PUBLIC void
print_count_level(Robot *mr)
{
    int i;
    HTPrint("Nodes per Level\n");
    for(i=0;i<= mr->depth; i++)
	if(mr->cdepth[i]!=0)
	    HTPrint("Level %d = %d\n", i, mr->cdepth[i]);
}



PUBLIC int 
HTKeywordFilter (HTRequest * request, HTResponse * response,
			    void * param, int status)
{
    HTParentAnchor *anchor =  HTRequest_anchor(request);
    char *keyw = NULL;
    /*HyperDoc * hd = HTAnchor_document((HTParentAnchor *)anchor);*/
    if (request) {
	HTLog * log = (HTLog *) param;

	if(!log)
	    return HT_ERROR;
	/* Check to see if we have keywords */

	if ((keyw = HTAssocList_findObject(HTAnchor_meta(anchor),
					   "keywords")) != NULL) {
	    char * strval = NULL;
	    char * ptr = NULL;
	    char * token = NULL;
	    char * uri = HTAnchor_address((HTAnchor *) anchor);
	    int first =1;
	    StrAllocCopy(strval, keyw);
	    ptr = strval;
	    while ((token = HTNextField(&ptr)) != NULL) {
		/*printf("META TOKEN %s %s %d\n",token,uri, hd ? hd->index:-1);*/
		if(!first)
		    HTLog_addText (log,"%s\n",token);
		else if (uri) {
		    HTLog_addText (log,"%s %s\n",token,uri);
		    first = 0;
		}
	    }
	    HT_FREE(strval); HT_FREE(uri);
	}
	return HT_OK;
    }
    return HT_ERROR;
}

PUBLIC char *
clean_string(char *str)
{
    char *nstr = NULL;
    if(str) {
	int l = strlen(str);
	int i;
	if((nstr = HT_CALLOC(1,l)) == NULL)
	    HT_OUTOFMEM("clean_string");
	strcpy(nstr,str);
	for(i=0;i<l;i++) {
	    if(!isascii(nstr[i]))
		nstr[i] = ' '; /* no asccii characters */
	    /*if(nstr[i]=='\n')
		nstr[i]=' ';*/
	    if(isspace((int)nstr[i]))
		nstr[i] = ' '; /* no space characters */
	}
    }
    return nstr;
}

#define HEX_ESCAPE '%'
#define ROB_ACCEPTABLE(a)	( a>=32 && a<128 )

PRIVATE char *hex = "0123456789ABCDEF";

PUBLIC char *
HTEscape_nonascii(const char * str)
{
    const char * p;
    char * q;
    char * result;
    int unacceptable = 0;
    if (!str) return NULL;
    for(p=str; *p; p++)
        if (!ROB_ACCEPTABLE((unsigned char)TOASCII(*p)))
		unacceptable++;
    if ((result = (char  *) HT_MALLOC(p-str + unacceptable+ unacceptable + 1)) == NULL)
        HT_OUTOFMEM("HTEscape_nonascii");
    for(q=result, p=str; *p; p++) {
    	unsigned char a = TOASCII(*p);
	if (!ROB_ACCEPTABLE(a)) {
	    *q++ = HEX_ESCAPE;	/* Means hex commming */
	    *q++ = hex[a >> 4];
	    *q++ = hex[a & 15];
	}
	else *q++ = *p;
    }
    *q++ = 0;			/* Terminate */
    return result;
}

#endif










